require(['jquery', 'cookie', 'app', "lay", "base64"], function ($) {
    require(['wangEditor'], function (E) {
        editor = new E("#note");
        editor.customConfig.uploadImgServer = configPro.server_img_interfacefr; //上传URL
        editor.customConfig.uploadImgMaxSize = 10 * 1024 * 1024;
        editor.customConfig.uploadImgMaxLength = 5;
        editor.customConfig.uploadFileName = 'myFileName';
        editor.customConfig.uploadImgHooks = {
            customInsert: function (insertImg, result, editor) {
                var url = configPro.server_img_view + result.data;
                insertImg(url);
            }
        };
        editor.create();
    });
    require(['vue'], function (Vue) {
        layui.use(['form', 'laydate', 'upload'], function () {
            var upload = layui.upload;
            var uploadInst = upload.render({
                elem: '#upload' //绑定元素
                , url: configPro.server_img_interface //上传接口
                , done: function (res) {
                    dic.that.image.push(configPro.server_img_view + res.data);
                }
                , error: function (res) {
                    alr("上传失败");
                }
                , accept: 'file'
            });
        });
        var dic = new Vue({
            el: ".vue-box"
            , data: {
                where: {},
                chu: {
                    shopId: "",
                    number: 0,
                    note: ""
                },
                that: {
                    name: "",
                    image: [],
                    icon: "",
                    status: false,
                    note: "",
                    makePrice: "",
                    price: "",
                    brand: "",
                    place: "",
                    classify: {},
                    ripe: 0
                }
            }
            , created: function () {
                this.loadPage();
            }
            , methods: {
                loadPage: function () {
                    load();
                    var toolbar = [];
                    toolbar.push({
                        "clas": "layui-icon layui-icon-add-1",
                        "eve": "ADD",
                        "tit": "新增",
                        "fnc": function (data) {
                            dic.openAdd();
                        }
                    });
                    var tool = [];
                    tool.push({
                        "clas": "layui-btn-danger",
                        "eve": "UPDATE",
                        "tit": "修改",
                        "fnc": function (data) {
                            dic.that = data.data;
                            dic.that['status'] = false;
                            dic.that['image'] = JSON.parse(dic.that['image']);
                            dic.that['note'] = Base64.decode(dic.that.note.replace(/ /g, "+"));
                            setTimeout(function () {
                                editor.txt.html("");
                                editor.txt.html(dic.that['note']);
                            }, 1500);
                            dic.openAdd();
                        }
                    });
                    tool.push({
                        "clas": "layui-btn-danger",
                        "eve": "UPDATE1",
                        "tit": "出库",
                        "fnc": function (data) {
                            dic.chu.shopId = data.data.id;
                            dic.openChu();
                        }
                    });
                    tool.push({
                        "clas": "layui-btn-danger",
                        "eve": "UPDATE2",
                        "tit": "入库",
                        "fnc": function (data) {
                            dic.chu.shopId = data.data.id;
                            dic.openRu();
                        }
                    });
                    bspVIEW($("#authoritiesTable"), "/service-shop/ShopController/getShop", [
                        {type: 'checkbox'},
                        {type: 'numbers'},
                        {field: 'id', title: 'id', minWidth: 120},
                        {field: 'name', title: '名称', minWidth: 120},
                        {
                            templet: '<p>{{d.classify.name}}</p>',
                            align: 'center',
                            width: 100,
                            title: '分类'
                            , minWidth: 120
                        },
                        {field: 'icon', title: '标签', minWidth: 120},
                        {field: 'unit', title: '单位', minWidth: 120},
                        {field: 'makePrice', title: '市场价', minWidth: 120},
                        {field: 'price', title: '现价', minWidth: 120},
                        {field: 'brand', title: '品牌', minWidth: 120},
                        {field: 'place', title: '产地', minWidth: 120},
                        {field: 'repertory', title: '库存', minWidth: 120},
                        {field: 'ripe', title: '成熟时间（s）', minWidth: 120},
                        {
                            templet: '<p>{{# if(d.status==false){ }} 禁用 {{# } }}{{# if(d.status==true){ }} 启用 {{# } }}</p>',
                            align: 'center',
                            width: 100,
                            title: '状态'
                            , minWidth: 120
                        },
                        {title: '操作', toolbar: '#authoritiesTbBar', align: 'center', minWidth: 240}
                    ], {common: this.where}, toolbar, tool, function (res, curr, count) {

                    });
                },
                seachs: function () {
                    this.where = fromName($(".el-form"));
                    this.loadPage();
                },
                refer: function () {

                },
                openAdd: function () {
                    alrYN($("#admins"), function () {
                        var jon = dic.that;
                        jon['image'] = JSON.stringify(jon['image']);
                        var xe = editor.txt.html();
                        xe = xe.replace(/&nbsp;/g, " ");
                        jon['note'] = Base64.encode(xe);
                        jon['status'] = $('input[name="status"]').next().find("em").text();
                        var ret = playPost("service-shop/ShopController/shop", "", JSON.stringify(jon));
                        alr(ret.msg);
                    }, function () {

                    }, function () {

                    }, "新增商品", 1, ["900px", "400px"]);
                },
                openRu: function () {
                    alrYN($("#chu"), function () {
                        var jon = dic.chu;
                        var ret = playPut("service-shop/RepertoryController/repertory", "", jon);
                        alr(ret.msg);
                    }, function () {

                    }, function () {

                    }, "入库", 1, ["600px", "380px"]);
                },
                openChu: function () {
                    alrYN($("#chu"), function () {
                        var jon = dic.chu;
                        var ret = playDelete("service-shop/RepertoryController/repertory", "", jon);
                        alr(ret.msg);
                    }, function () {

                    }, function () {

                    }, "出库", 1, ["600px", "380px"]);
                },
                delNote: function (i) {
                    var jon = dic.that;
                    jon['image'].splice(i, 1);
                    this.that = jon;
                }
            }
        });
    })
});